package android.slc.commonlibrary.util.compat;

import com.blankj.utilcode.util.ArrayUtils;

import java.util.Calendar;

/**
 * 时间工具
 * 包括时间格式化等等工具
 */
public class SlcTimeUtils {

    public final static int VALUE_MINUTE_WITH_SECOND = 60;//1分钟/秒单位
    public final static int VALUE_MINUTE_WITH_MILLIS = 60 * 1000;//一分钟/毫秒单位
    public final static int VALUE_HOUR_WITH_MILLIS = VALUE_MINUTE_WITH_MILLIS * 60;
    public final static int VALUE_HOUR_WITH_MINUTE = 60;//1小时/分单位
    public final static int VALUE_DAY_WITH_MILLIS = VALUE_HOUR_WITH_MILLIS * 24;

    /**
     * 清除日历
     *
     * @param calendar
     * @param field
     */
    public static void clearCalendarField(Calendar calendar, int... field) {
        if (calendar != null) {
            ArrayUtils.forAllDo(field, (ArrayUtils.Closure<Integer>) (index, item) -> calendar.set(item, 0));
        }
    }

    public static long clearCalendarField(long timeInMillis, int... field) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timeInMillis);
        clearCalendarField(calendar, field);
        return calendar.getTimeInMillis();
    }

    /**
     * 时间转int数值
     *
     * @param time
     * @return
     */
    public static int[] hourStr2IntArray(String time) {
        String[] timeStrArray = time.split(":");
        int[] intArray = new int[3];
        switch (timeStrArray.length) {
            case 3:
                intArray[2] = Integer.parseInt(timeStrArray[2]);
            case 2:
                intArray[1] = Integer.parseInt(timeStrArray[1]);
            case 1:
                intArray[0] = Integer.parseInt(timeStrArray[0]);
                break;
        }
        return intArray;
    }

    /**
     * 毫秒转小时
     *
     * @param millisecond
     * @return
     */
    public static float millisecond2Hour(long millisecond) {
        return millisecond / (float) VALUE_HOUR_WITH_MILLIS;
    }

    /**
     * 小时转毫秒
     *
     * @param hour
     * @return
     */
    public static long hour2Millisecond(float hour) {
        return (long) (hour * VALUE_HOUR_WITH_MILLIS);
    }

    /**
     * 分钟转小时
     *
     * @param minute
     * @return
     */
    public static float minute2Hour(int minute) {
        return (float) minute / VALUE_HOUR_WITH_MINUTE;
    }
}
